package com.disk.controller;

import com.disk.domain.FolderInformation;
import com.disk.handleResult.FolderResult;
import com.disk.handleResult.Result;
import com.disk.service.FolderService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@PreAuthorize("hasAuthority('manage:folder')")
@RestController
@RequestMapping("/folder")
public class FolderController {
    @Autowired
    private FolderService folderService;

    @Operation(summary = "通过Authentication获取用户名后直接设置到路径中" +
            "，使各用户在其所属文件夹下操作")
    @GetMapping("/get")
    public Object getCurrentUserName(Authentication authentication){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        folderService.setUserRoot(userDetails.getUsername());
        return userDetails.getUsername();
    }

    @Operation(summary = "获取网盘容量")
    @GetMapping("/space")
    public Result getSpaceInfo() {
        return folderService.getSpaceInfo();
    }

    @Operation(summary = "根据folderName创建文件夹")
    @GetMapping("create")
    public FolderResult createFolder(@RequestParam String folderName) {
        return folderService.createFolder(folderName);
    }

    @Operation(summary = "根据folderName删除文件夹，" +
            "all为Y/yes/true时删除文件夹下所属文件")
    @GetMapping("/delete")
    public FolderResult deleteFolder(@RequestParam String folderName,
                                     @RequestParam(required = false,defaultValue = "yes") String all) {
        return folderService.deleteFolder(folderName,all);
    }

    @Operation(summary = "传入folder对象，更新文件夹(重命名)")
    @PostMapping("/update")
    public FolderResult updateFolder(@RequestBody FolderInformation folderInformation) {
        return folderService.updateFolder(folderInformation);
    }

    @Operation(summary = "获取文件夹下的所有内容，" +
            "all为Y/yes/ture时获取包含文件夹的所有内容，否则只获取文件")
    @GetMapping("/children")
    public FolderResult getFiles(@RequestParam String folderName,
                                 @RequestParam(required = false,defaultValue = "yes") String all) {
        return folderService.getFiles(folderName,all);
    }
}
